package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjStoreOutboundReceiptSend;
import com.zb.mapper.ZjStoreOutboundReceiptSendMapper;
import com.zb.service.ZjStoreOutboundReceiptSendService;
import com.zb.vo.StoreOutboundReceiptVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 出库收发货表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2024-01-08
 */
@Service
public class ZjStoreOutboundReceiptSendServiceImpl extends ServiceImpl<ZjStoreOutboundReceiptSendMapper, ZjStoreOutboundReceiptSend> implements ZjStoreOutboundReceiptSendService {

    // 保存出库收发货人信息
    @Override
    public void save(Long custAccId, Long id, StoreOutboundReceiptVo receiptVo) {
        ZjStoreOutboundReceiptSend receiptSend = new ZjStoreOutboundReceiptSend();
        BeanUtil.copyProperties(receiptVo, receiptSend);
        receiptSend.setId(id);
        if (null == this.getById(id)) {
            receiptSend.setCustAccId(custAccId);
            receiptSend.setSendName("");
            receiptSend.setSendPhone("");
            receiptSend.setSendProvince("");
            receiptSend.setSendCity("");
            receiptSend.setSendArea("");
            receiptSend.setSendTown("");
            receiptSend.setSendAddress("");
            this.save(receiptSend);
            return;
        }
        this.updateById(receiptSend);
    }

    // 根据删除的客户ID获取出库收货客户ID
    @Override
    public List<Long> getUserIdByDelUserId(List<Long> userId) {
        if (CollectionUtil.isEmpty(userId)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjStoreOutboundReceiptSend> wrapper = new QueryWrapper<>();
        wrapper.last("limit 10")
                .select("relation_id")
                .in("relation_id", userId).eq("receipt_addr_type", 1);
        return this.list(wrapper).stream().map(ZjStoreOutboundReceiptSend::getRelationId).distinct().toList();
    }
}
